<?php

namespace app\controller;

use app\BaseController;
use app\model\User;
use think\Request;
use app\validator\UserSave;

//  用户接口
class UserController extends BaseController{
    public function info(Request $request){
        $user = $request->user;
        if(!$user)
            return json([
                'code' => 404,
                'msg' => '获取失败'
            ],404);
        return json([
            'code' => 200,
            'msg' => '获取成功',
            'data' => $user
        ]);
    }

    public function list(Request $request){
        $limit = $request->param('limit') ?? 10;
        $page = $request->param('page') ?? 1;
        $name = $request->param('name') ?? '';
        $user = new User();
        if($name){
            $paginate = $user->with('roles')
                ->where('name','like','%'.$name.'%')
                ->field('id,name,status,last_login_time,last_login_ip')
                ->paginate([
                    'list_rows' => $limit,
                    'page' => $page,
                ]);
        } else {
            $paginate = $user->with('roles')
                ->field('id,name,status,last_login_time,last_login_ip')
                ->paginate([
                    'list_rows' => $limit,
                    'page' => $page,
                ]);
        }
        $list = $paginate->items();
        $total = $paginate->total();


        if (empty($list)) {
            return json([
                'code' => 400,
                'msg' => '暂无数据',
                'total' => 0,
                'data' => []
            ],400);
        }
        $data = [];
        foreach ($list as $user) {
            $userData = $user->toArray();
            $roles = $user->roles;
            $roleNames = [];
            $roleDescriptions = [];
            foreach ($roles as $role) {
                $roleNames[] = $role->name;
                $roleDescriptions[] = $role->description;
            }
            $userData['role_names'] = $roleNames;
            $userData['role_descriptions'] = $roleDescriptions;
            unset($userData['roles']);
            $data[] = $userData;
        }
        return json([
            'code' => 200,
            'msg' => '获取成功',
            'total' => $total,
            'data' => $data
        ]);
    }

    public function save(Request $request){
        $data = $request->param();

        $validate = new UserSave();
        if (isset($data['id']) && intval($data['id']) >= 0)
            $scene = 'update'; // 更新数据
        else
            $scene = 'create';  // 添加数据

        //  验证参数
        if (!$validate->scene($scene)->check($data)) {
            return json([
                'code' => 400,
                'msg' => $validate->getError()
            ], 400);
        }

        $id =  $data['id'] ?? 0;
        $name = $data['name'];
        $password = $data['password'];
        $status = $data['status'];
        $last_login_time = $data['last_login_time'];
        $last_login_ip = $data['last_login_ip'];

        if ($id > 0) {
            // 更新数据
            $user = new User();
            $fields = [
                'id',
                'name',
                'password',
                'status',
                'last_login_ip',
                'last_login_time'
            ];
            $values = [
                $id,    
                $name,
                password_hash($password, PASSWORD_DEFAULT),
                $status,
                $last_login_ip,
                $last_login_time
            ];
            $data = $user->_update($fields, $values);
            return json($data,$data['code']);
        } else {
            // 新增数据
            $user = new User();
            $fields = [
                'name',
                'password',
                'status',
                'last_login_ip',
                'last_login_time'
            ];
            $values = [  
                $name,
                password_hash($password, PASSWORD_DEFAULT),
                $status,
                $last_login_ip,
                $last_login_time
            ];
            $data = $user->_create($fields, $values);
            return json($data,$data['code']);
        }
    }

    public function delete(Request $request){
        $id = $request->param('id');
        $user = new User();
        $data = $user->_delete($id);
        return json($data,$data['code']);
    }

    public function all_delete(Request $request){
        $ids = $request->param('idArray');
        $user = new User();
        $data = $user->_allDelete($ids);
        return json($data,$data['code']);
    }
}